package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author 杨旭康
 * @Date 2022/11/2 23:29
 * @question:
 */

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * 请求 URL: http://localhost:8080/order/submit
     * 请求方法: POST
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {

        log.info("订单数据: {}",orders);
        orderService.submit(orders);

        return R.success("下单成功");
    }

    /**
     * 查看订单
     * 请求 URL: http://localhost:8080/order/userPage?page=1&pageSize=1
     * 请求方法: GET
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> pageR(int page,int pageSize) {

        //获取用户id
        Long userId = BaseContext.getCurrentId();

        Page<Orders> pageInfo = new Page<>(page,pageSize);
        //将所有数据拷到dto当中
        Page<OrdersDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId,userId);
        wrapper.orderByAsc(Orders::getOrderTime);

        orderService.page(pageInfo,wrapper);
        //开始拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> ordersDtoList = records.stream().map((item) -> {
            //创建一个空的dto对象 用来取值 设置值
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);

            //将订单明细(菜品/套餐名称等加载进来)
            LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
            //根据id查询匹配了多少订单明细
            queryWrapper.eq(OrderDetail::getOrderId,item.getId());
            //调用service查询
            List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper);
            //设置进去
            ordersDto.setOrderDetails(orderDetails);

            return ordersDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(ordersDtoList);

        return R.success(dtoPage);
    }

    /**
     * 管理端的 订单明细
     *
     * 请求 URL: http://localhost:8080/order/page
     * ?page=1
     * &pageSize=10
     * &number=123
     * &beginTime=2022-11-08%2000%3A00%3A00
     * &endTime=2022-12-15%2023%3A59%3A59
     *
     * 请求方法: GET
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageR(int page,int pageSize,Long number,String beginTime,String endTime) {

        //此处的 beginTime endTime 直接使用字符串接即可 对比的是订单的orderTime下单时间
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        //创建条件查询器
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null != number,Orders::getNumber,number)
                .between(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime),Orders::getOrderTime,beginTime,endTime)
                .orderByAsc(Orders::getOrderTime);

        //调用service查询
        orderService.page(pageInfo,wrapper);

        return R.success(pageInfo);
    }

    /**
     * 更改订单状态  直接更改即可
     * 请求 URL: http://localhost:8080/order
     * 请求方法: PUT
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> putR(@RequestBody Orders orders) {

        log.info("更改订单状态 : {} ",orders);

        orderService.updateById(orders);

        return R.success("订单状态更改成功");
    }

    /**
     * 再来一单
     * 请求 URL: http://localhost:8080/order/again
     * 请求方法: POST
     */

    @PostMapping("/again")
    public R<String> againR(@RequestBody Orders orders) {

        orderService.saveAgain(orders);

        return R.success("第二次下单成功~");
    }







}
